<?php

namespace App\Jobs;

use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;

class TestJob extends Job
{
    public array $data;
    public function __construct(array $data = [])
    {
        $this->data = $data;
    }


    public function handle(): void
    {
        Log::channel('base')->info('=====任务开始====='.date('Y-m-d H:i:s'));

        $res = $this->callback(
            'https://learn.yuxibs.com/api/callback',
            [
                'json' => [
                    'code' => 200,
                    'msg' => 'success',
                    'data' => [
                        'id' => 1,
                        'name' => 'test',
                    ],
                ],
            ]
        );

        Log::channel('base')->info(json_encode($res));

    }

    public function callback($url, $params): bool
    {
        try {
            $response = Http::withHeaders([
                'accept' => 'application/json',
            ])->post($url, $params);
            Log::channel('base')->info('推送成功:'.json_encode($params));
            return true;
        }catch (\Exception $exception){
            Log::channel('base')->info($exception->getMessage());
            return false;
        }
    }
}
